package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.PlaybackDialogFragment;
import palmtop.com.palmtop.util.RecordingItem;

public class LookDetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.pname)
    AwesomeTextView pname;
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.taskaddress)
    AwesomeTextView taskaddress;
    @BindView(R.id.pnote)
    AwesomeTextView pnote;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.status)
    AwesomeTextView status;
    @BindView(R.id.slay)
    ScrollView slay;
    @BindView(R.id.footBtn)
    Button footBtn;
    @BindView(R.id.mainlay)
    LinearLayout mainlay;
    Processtask processtask;
    @BindView(R.id.play)
    Button play;
    MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_detail, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(LookDetailActivity.this);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        app.addActivity(LookDetailActivity.this);
        init();
    }

    private void init() {
        processtask = (Processtask) getIntent().getSerializableExtra("processtask");
        if (processtask != null) {
            if(TextUtils.isEmpty(processtask.getRecordurl())){
                play.setEnabled(false);
            }
            if (processtask.getStatus().equals("2")) {
                footBtn.setText("重新上传");
                status.setText("驳回需重新上传");
            } else if (processtask.getStatus().equals("0")) {
                footBtn.setVisibility(View.GONE);
                status.setText("已提交任务");
            } else if (processtask.getStatus().equals("1")) {
                footBtn.setVisibility(View.GONE);
                status.setText("已确认");
            } else if (processtask.getStatus().equals("3")) {
                footBtn.setVisibility(View.GONE);
                status.setText("已结束任务");
            }
            pname.setText(processtask.getPname());
            addtime.setText(processtask.getAddtime());
            taskaddress.setText(processtask.getTaskaddress());
            pnote.setText(processtask.getPnote());

            if (processtask.getPhoto1() != null && !processtask.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + processtask.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto1(), zphoto1, LookDetailActivity.this);
            }
            if (processtask.getPhoto2() != null && !processtask.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + processtask.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto2(), zphoto2, LookDetailActivity.this);
            }
            if (processtask.getPhoto3() != null && !processtask.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + processtask.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto3(), zphoto3, LookDetailActivity.this);
            }
            if (processtask.getPhoto4() != null && !processtask.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + processtask.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + processtask.getPhoto4(), zphoto4, LookDetailActivity.this);
            }

        }

    }

    @OnClick(R.id.footBtn)
    public void onViewClicked() {
        Intent i = new Intent();
        i.putExtra("processtask", processtask);
        i.setClass(LookDetailActivity.this, DotaskssActivity.class);
        startActivity(i);

    }

    @OnClick(R.id.play)
    public void oncliked() {
        RecordingItem recordingItem = new RecordingItem();
        SharedPreferences sharePreferences = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
        String filePath = Config.base_url+processtask.getRecordurl();
        int elpased = Integer.valueOf(processtask.getRecordlength());
        recordingItem.setFilePath(filePath);
        recordingItem.setLength(elpased);
//        recordingItem.setLength((int)6);
        PlaybackDialogFragment fragmentPlay = PlaybackDialogFragment.newInstance(recordingItem);
        fragmentPlay.show(getSupportFragmentManager(), PlaybackDialogFragment.class.getSimpleName());

    }
}
